//index.js
//获取应用实例
const app = getApp()
var zsSuit = require('../../utils/ZsSuit.js');

Page({
  data: {
    list:[
        {name:'成色',option:[{name:'7成新',num:'0',id:'1'},{name:'8成新',num:'0',id:'2'},{name:'9成新',num:'0',id:'3'},{name:'全新',num:'0',id:'4'}]},
        {name:'颜色',option:[{name:'白色',num:'1',id:'5'},{name:'金色',num:'1',id:'6'},{name:'黑色',num:'1',id:'7'}]},
        {name:'内存',option:[{name:'256GB',num:'2',id:'10'},{name:'128GB',num:'2',id:'11'}]}
    ],
    yixuan:{}
  },
  onLoad: function () {
     var suitRuleInfo = {"123":"1_6_10","1235":"1_5_10","12351":"1_7_10","12251":"2_7_10","122151":"2_5_10","1221516":"2_6_10","234":"1_7_11","345":"2_7_10","456":"3_5_11","789":"4_5_10"}; //已有套装
     zsSuit.config({'suitRuleInfo':suitRuleInfo});     
     var self = this;
     zsSuit.callBack = function(data, skuId){
            //返回所有不可选data
           // console.log(data);
            if(data.length==0)return;
            var list = self.data.list;
            for(var i in data){
                var optionval=list[i]['option'];
                for(var r in optionval){
                    var curVal = optionval[r].id;
                    if(zsSuit.inArr(curVal, data[i]) < 0){
                        optionval[r].disabled=0;
                    }else{
                        optionval[r].disabled=1;
                    }
                }
            }
            self.setData({
                list: list
            });            
    };
  },
  cviewck:function(e){
    var self = this;
    var curVal          =  e.currentTarget.dataset.optionvalueid;// 选中的值
    var curKey          =  e.currentTarget.dataset.key;// 选中的值 键名
    var position        =  e.currentTarget.dataset.num;// 层级
    var chooseFlag      =  self.data.list[position]['option'][curKey].current;//是否已选中
    var canNotClick     =  self.data.list[position]['option'][curKey].disabled;//是否不可选
    //是否可点击
    if(canNotClick){return false;}
    //判断选择
    if(chooseFlag){
        self.chooseCancel(1,position,curVal);//样式变化
        zsSuit.unset(position, curVal);
    }else{
        var yixuan = self.data.yixuan;
        yixuan[position]=curVal;
            self.setData({
            yixuan: yixuan
          })
    	self.chooseCancel(2,position,curVal);//重新选中
    	zsSuit.set(position,curVal);
    }
  },//套装选择导致的样式变化
  chooseCancel:function(type,position,val){
    var self = this;
    var list = self.data.list;
    var optionval=list[position]['option'];
    for(var i in optionval){
        var curVal = optionval[i].id;
        if(curVal == val){
            //type操作 1取消 2选中
            if(type == 1){
                optionval[i].current=0;
            }else{
                optionval[i].current=1;
            }
        }else{
            if(type != 1)optionval[i].current=0;
        }
    }
    self.setData({
        list: list
    });
  }
})